/**
 * Created by liuqingxia on 2016/6/29.
 */


/**
 * 查找相匹配的表名
 * @param target
 */
function clickSearchTableNameBtn(btnId, inputId, prefix) {

    $(document).on("click", "#" + btnId, function () {

        var tableName = $.trim($("#" + inputId).val());
        triggerTableNameLi(prefix, tableName);
    });
}

/**
 * 触发TableNameLiSelect
 * @param tableName
 */
function triggerTableNameLi(prefix, tableName) {

    var findMatchTableName = 0;//0代表没有找到相匹配的表，1代表找到相匹配的表

    $.each($("li." + prefix + "TableNameLi"), function () {

        var tableNameSpan = $(this).find("span.text");
        var eachTableName = $.trim(tableNameSpan.html());

        tableName = tableName.toLowerCase();
        eachTableName = eachTableName.toLowerCase();

        if (tableName == eachTableName) {
            findMatchTableName = 1;
            try {
                $(this).trigger("click");
            } catch (e) {

            }
            return false;
        }

        return true;
    });

    if (0 == findMatchTableName) {
        swal("提示", "没有找到匹配的表名", "error");
    }
}


/**
 *searchTableNameInput输入值变化
 */
function searchTableNameInputChange(inputId, prefix, containerId, ulId) {

    $("#" + inputId).bind('input propertychange', function () {

        var inputTableName = $.trim($(this).val()).toLowerCase();
        var matchLiTableNames = [];

        $.each($("li." + prefix + "TableNameLi"), function () {

            var tableNameSpan = $(this).find("span.text");
            var eachTableName = $.trim(tableNameSpan.html());

            if (eachTableName.toLowerCase().contains(inputTableName)) {
                matchLiTableNames.push(eachTableName);
            }
            return true;
        });

        showRelatedTableNames(matchLiTableNames, prefix, containerId, ulId);
    });
}

/**
 * 输入框内容变化时，展示相关联的tableNames
 * @param matchLiTableNames
 */
function showRelatedTableNames(matchLiTableNames, searchTableNameLiPre, containerId, ulId) {

    var length = matchLiTableNames.length;

    if (0 == length) {
        hideRelatedTableNamesDiv(containerId);
        return;
    }

    var lis = "";

    for (var i = 0; i < length; i++) {

        lis += "" +
            "<li class='" + searchTableNameLiPre + "SearchTableNameLi'>" +
            "<a> " +
            "<span class = 'text'>" +
            matchLiTableNames[i] +
            "</span> " +
            "<span class= 'glyphicon glyphicon-ok check-mark'>" +
            "</span>" +
            "</a>" +
            "</li>";
    }

    appendTableNamesLi(lis, ulId);
    showRelatedTableNamesDiv(containerId);

    return;
}


function appendTableNamesLi(lis, ulId) {
    var relatedTableNamesUl = $("#" + ulId);
    relatedTableNamesUl.empty();
    relatedTableNamesUl.append(lis);
}

function hideRelatedTableNamesDiv(divId) {
    var relatedTableNamesDiv = $("#" + divId);
    relatedTableNamesDiv.hide();
}

function showRelatedTableNamesDiv(divId) {
    var relatedTableNamesDiv = $("#" + divId);
    relatedTableNamesDiv.show();
}

function clickRelatedTableNameLi(prefix, relatedTableNameDivId) {
    $(document).on("click", "." + prefix + "SearchTableNameLi", function () {
        var tableName = $.trim($(this).find("span.text").html());
        triggerTableNameLi(prefix, tableName);
        hideRelatedTableNamesDiv(relatedTableNameDivId);
    });
}

